//<<<+++OPENSOURCE
//<<<+++OPENSOURCE_LICENSE
/* FreeType interface */

//#define _EXTERNAL_FREETYPE_

#if !defined(_EXTERNAL_FREETYPE_) && !defined(_FPDF_TEXTONLY_)
// Refer to FreeType header files
#ifndef _FXM_OPENSOURCE_
//<<<+++OPENSOURCE_BEGIN LIC==FOXIT||LIC==GOOGLE
#include "../thirdparties/freetype/ft2build.h"
#include "../thirdparties/freetype/freetype/freetype.h"
#include "../thirdparties/freetype/freetype/ftoutln.h"
#include "../thirdparties/freetype/freetype/ftmm.h"
#include "../thirdparties/freetype/freetype/internal/ftobjs.h"
#include "../thirdparties/freetype/freetype/tttables.h"
//<<<+++OPENSOURCE_END
#endif

//<<<+++OPENSOURCE_MUST_BEGIN
#ifdef __cplusplus
extern "C" {
#endif
//<<<+++OPENSOURCE_MUST_END
// Use FreeType definitions
#define FXFT_ENCODING_UNICODE FT_ENCODING_UNICODE
#define FXFT_ENCODING_ADOBE_STANDARD FT_ENCODING_ADOBE_STANDARD
#define FXFT_ENCODING_ADOBE_EXPERT FT_ENCODING_ADOBE_EXPERT
#define FXFT_ENCODING_ADOBE_LATIN_1 FT_ENCODING_ADOBE_LATIN_1
#define FXFT_ENCODING_APPLE_ROMAN FT_ENCODING_APPLE_ROMAN
#define FXFT_ENCODING_ADOBE_CUSTOM FT_ENCODING_ADOBE_CUSTOM
#define FXFT_ENCODING_MS_SYMBOL FT_ENCODING_MS_SYMBOL
#define FXFT_ENCODING_GB2312 FT_ENCODING_GB2312
#define FXFT_ENCODING_BIG5 FT_ENCODING_BIG5
#define FXFT_ENCODING_SJIS FT_ENCODING_SJIS
#define FXFT_ENCODING_JOHAB FT_ENCODING_JOHAB
#define FXFT_ENCODING_WANSUNG FT_ENCODING_WANSUNG
#define FXFT_LOAD_NO_SCALE FT_LOAD_NO_SCALE
#define FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
#define FXFT_RENDER_MODE_LCD FT_RENDER_MODE_LCD
#define FXFT_RENDER_MODE_MONO FT_RENDER_MODE_MONO
#define FXFT_RENDER_MODE_NORMAL FT_RENDER_MODE_NORMAL
#define FXFT_LOAD_IGNORE_TRANSFORM FT_LOAD_IGNORE_TRANSFORM
#define FXFT_LOAD_NO_BITMAP FT_LOAD_NO_BITMAP
#define FXFT_LOAD_NO_HINTING FT_LOAD_NO_HINTING
#define FXFT_PIXEL_MODE_MONO FT_PIXEL_MODE_MONO
#define FXFT_STYLE_FLAG_ITALIC FT_STYLE_FLAG_ITALIC
#define FXFT_STYLE_FLAG_BOLD FT_STYLE_FLAG_BOLD
#define FXFT_FACE_FLAG_SFNT FT_FACE_FLAG_SFNT
#define FXFT_FACE_FLAG_TRICKY ( 1L << 13 )	/* Until 242, Freetype introduce the concept of tricky font */

typedef FT_MM_Var*		FXFT_MM_Var;
typedef FT_Bitmap*		FXFT_Bitmap;
#define FXFT_Matrix		FT_Matrix
#define FXFT_Vector		FT_Vector
#define FXFT_Outline_Funcs FT_Outline_Funcs
typedef FT_Open_Args	FXFT_Open_Args;
typedef FT_StreamRec	FXFT_StreamRec;
typedef FT_StreamRec*	FXFT_Stream;
typedef FT_BBox			FXFT_BBox;
typedef FT_Glyph		FXFT_Glyph;
typedef FT_CharMap		FXFT_CharMap;
#define FXFT_GLYPH_BBOX_PIXELS	FT_GLYPH_BBOX_PIXELS

#define FXFT_Open_Face(library, args, index, face) \
			FT_Open_Face((FT_Library)(library), args, index, (FT_Face*)(face))
#define FXFT_Done_Face(face) FT_Done_Face((FT_Face)(face))
#define FXFT_Done_FreeType(library) FT_Done_FreeType((FT_Library)(library))
#define FXFT_Init_FreeType(library) FT_Init_FreeType((FT_Library*)(library))
#define FXFT_New_Memory_Face(library, base, size, index, face) \
			FT_New_Memory_Face((FT_Library)(library), base, size, index, (FT_Face*)(face))
#define FXFT_New_Face(library, filename, index, face) \
			FT_New_Face((FT_Library)(library), filename, index, (FT_Face*)(face))
#define FXFT_Get_Face_FreeType(face) ((FT_Face)face)->driver->root.library
#define FXFT_Select_Charmap(face, encoding) FT_Select_Charmap((FT_Face)face, (FT_Encoding)encoding)
#define FXFT_Set_Charmap(face, charmap) FT_Set_Charmap((FT_Face)face, (FT_CharMap)charmap)
#define FXFT_Load_Glyph(face, glyph_index, flags) FT_Load_Glyph((FT_Face)face, glyph_index, flags)
#define FXFT_Get_Char_Index(face, code) FT_Get_Char_Index((FT_Face)face, code)
#define FXFT_Get_Glyph_Name(face, index, buffer, size) FT_Get_Glyph_Name((FT_Face)face, index, buffer, size)
#define FXFT_Get_Name_Index(face, name) FT_Get_Name_Index((FT_Face)face, name)
#define FXFT_Has_Glyph_Names(face)  (((FT_Face)face)->face_flags & FT_FACE_FLAG_GLYPH_NAMES)
#define FXFT_Get_Postscript_Name(face) FT_Get_Postscript_Name((FT_Face)face)
#define FXFT_Load_Sfnt_Table(face, tag, offset, buffer, length) \
	         FT_Load_Sfnt_Table((FT_Face)face, tag, offset, buffer, length)  
#define FXFT_Get_First_Char(face, glyph_index) FT_Get_First_Char((FT_Face)face, glyph_index)
#define FXFT_Get_Next_Char(face, code, glyph_index) FT_Get_Next_Char((FT_Face)face, code, glyph_index)

#define FXFT_Clear_Face_External_Stream(face) (((FT_Face)face)->face_flags&=~FT_FACE_FLAG_EXTERNAL_STREAM)
#define FXFT_Get_Face_External_Stream(face) (((FT_Face)face)->face_flags&FT_FACE_FLAG_EXTERNAL_STREAM)
#define FXFT_Is_Face_TT_OT(face) (((FT_Face)face)->face_flags&FT_FACE_FLAG_SFNT)
#define FXFT_Is_Face_Tricky(face) (((FT_Face)face)->face_flags&FXFT_FACE_FLAG_TRICKY)
#define FXFT_Is_Face_fixedwidth(face) (((FT_Face)face)->face_flags & FT_FACE_FLAG_FIXED_WIDTH)
#define FXFT_Get_Face_Stream_Base(face) ((FT_Face)face)->stream->base
#define FXFT_Get_Face_Stream_Size(face) ((FT_Face)face)->stream->size
#define FXFT_Get_Face_Family_Name(face) ((FT_Face)face)->family_name
#define FXFT_Get_Face_Style_Name(face) ((FT_Face)face)->style_name
#define FXFT_Get_Face_Numfaces(face) ((FT_Face)face)->num_faces
#define FXFT_Get_Face_Faceindex(face) ((FT_Face)face)->face_index
#define FXFT_Is_Face_Italic(face) (((FT_Face)face)->style_flags&FT_STYLE_FLAG_ITALIC)
#define FXFT_Is_Face_Bold(face) (((FT_Face)face)->style_flags&FT_STYLE_FLAG_BOLD)
#define FXFT_Get_Face_Charmaps(face) ((FT_Face)face)->charmaps
#define FXFT_Get_Glyph_HoriBearingX(face) ((FT_Face)face)->glyph->metrics.horiBearingX
#define FXFT_Get_Glyph_HoriBearingY(face) ((FT_Face)face)->glyph->metrics.horiBearingY
#define FXFT_Get_Glyph_Width(face) ((FT_Face)face)->glyph->metrics.width
#define FXFT_Get_Glyph_Height(face) ((FT_Face)face)->glyph->metrics.height
#define FXFT_Get_Face_CharmapCount(face) ((FT_Face)face)->num_charmaps
#define FXFT_Get_Charmap_Encoding(charmap) ((FT_CharMap)charmap)->encoding
#define FXFT_Get_Face_Charmap(face) ((FT_Face)face)->charmap
#define FXFT_Get_Charmap_PlatformID(charmap) ((FT_CharMap)charmap)->platform_id
#define FXFT_Get_Charmap_EncodingID(charmap) ((FT_CharMap)charmap)->encoding_id
#define FXFT_Get_Face_UnitsPerEM(face) ((FT_Face)face)->units_per_EM
#define FXFT_Get_Face_xMin(face) ((FT_Face)face)->bbox.xMin
#define FXFT_Get_Face_xMax(face) ((FT_Face)face)->bbox.xMax
#define FXFT_Get_Face_yMin(face) ((FT_Face)face)->bbox.yMin
#define FXFT_Get_Face_yMax(face) ((FT_Face)face)->bbox.yMax
#define FXFT_Get_Face_Height(face) ((FT_Face)face)->height
#define FXFT_Get_Face_UnderLineThickness(face) ((FT_Face)face)->underline_thickness
#define FXFT_Get_Face_UnderLinePosition(face) ((FT_Face)face)->underline_position
#define FXFT_Get_Face_MaxAdvanceWidth(face) ((FT_Face)face)->max_advance_width
#define FXFT_Get_Face_Ascender(face) ((FT_Face)face)->ascender
#define FXFT_Get_Face_Descender(face) ((FT_Face)face)->descender
#define FXFT_Get_Glyph_HoriAdvance(face) ((FT_Face)face)->glyph->metrics.horiAdvance
#define FXFT_Get_MM_Axis(var, index) &((FT_MM_Var*)var)->axis[index]
#define FXFT_Get_MM_Axis_Min(axis) ((FT_Var_Axis*)axis)->minimum
#define FXFT_Get_MM_Axis_Max(axis) ((FT_Var_Axis*)axis)->maximum
#define FXFT_Get_MM_Axis_Def(axis) ((FT_Var_Axis*)axis)->def
#define FXFT_Get_Face_Internal_Flag(face) ((FT_Face)face)->internal->transform_flags
#define FXFT_Set_Face_Internal_Flag(face, flag) (((FT_Face)face)->internal->transform_flags = flag)
#define FXFT_Alloc(library, size) ((FT_Library)library)->memory->alloc(((FT_Library)library)->memory, size)
#define FXFT_Free(face, p) ((FT_Face)face)->memory->free(((FT_Face)face)->memory, p)
#define FXFT_Get_Glyph_Outline(face) &((FT_Face)face)->glyph->outline
#define FXFT_Get_Outline_Bbox(outline, cbox)  FT_Outline_Get_CBox(outline, cbox)
#define FXFT_Render_Glyph(face, mode) FT_Render_Glyph(((FT_Face)face)->glyph, (enum FT_Render_Mode_)mode)
#define FXFT_Get_MM_Var(face, p) FT_Get_MM_Var((FT_Face)face, p)
#define FXFT_Set_MM_Design_Coordinates(face, n, p) FT_Set_MM_Design_Coordinates((FT_Face)face, n, p)
#define FXFT_Set_Pixel_Sizes(face, w, h) FT_Set_Pixel_Sizes((FT_Face)face, w, h)
#define FXFT_Set_Transform(face, m, d) FT_Set_Transform((FT_Face)face, m, d)
#define FXFT_Outline_Embolden(outline, s) FT_Outline_Embolden(outline, s)
#define FXFT_Get_Glyph_Bitmap(face) &((FT_Face)face)->glyph->bitmap
#define FXFT_Get_Bitmap_Width(bitmap) ((FT_Bitmap*)bitmap)->width
#define FXFT_Get_Bitmap_Rows(bitmap) ((FT_Bitmap*)bitmap)->rows
#define FXFT_Get_Bitmap_PixelMode(bitmap) ((FT_Bitmap*)bitmap)->pixel_mode
#define FXFT_Get_Bitmap_Pitch(bitmap) ((FT_Bitmap*)bitmap)->pitch
#define FXFT_Get_Bitmap_Buffer(bitmap) ((FT_Bitmap*)bitmap)->buffer
#define FXFT_Get_Glyph_BitmapLeft(face) ((FT_Face)face)->glyph->bitmap_left
#define FXFT_Get_Glyph_BitmapTop(face) ((FT_Face)face)->glyph->bitmap_top
#define FXFT_Outline_Decompose(outline, funcs, params) FT_Outline_Decompose(outline, funcs, params)
#define FXFT_Set_Char_Size(face, char_width, char_height, horz_resolution, vert_resolution) FT_Set_Char_Size(face, char_width, char_height, horz_resolution, vert_resolution)
#define FXFT_Get_Glyph(slot, aglyph) FT_Get_Glyph(slot, aglyph)
#define FXFT_Glyph_Get_CBox(glyph, bbox_mode, acbox) FT_Glyph_Get_CBox(glyph, bbox_mode, acbox)
#define FXFT_Done_Glyph(glyph)	FT_Done_Glyph(glyph)
#define FXFT_Library_SetLcdFilter(library, filter) FT_Library_SetLcdFilter((FT_Library)(library), filter)

int FXFT_unicode_from_adobe_name(const char* name);
void FXFT_adobe_name_from_unicode(char* name, unsigned int unicode);
//<<<+++OPENSOURCE_MUST_BEGIN
#ifdef __cplusplus
};
#endif
//<<<+++OPENSOURCE_MUST_END

#else		// #if !defined(_EXTERNAL_FREETYPE_) && !defined(_FPDF_TEXTONLY_)
//<<<+++OPENSOURCE_BEGIN LIC==FOXIT
#ifdef __cplusplus
extern "C" {
#endif
// We can't use FreeType headers
#define FXFT_ENC_TAG(a, b, c, d ) ((a << 24) | (b << 16 ) | (c <<  8) | d)

const int FXFT_ENCODING_UNICODE = FXFT_ENC_TAG('u', 'n', 'i', 'c' );
const int FXFT_ENCODING_ADOBE_STANDARD = FXFT_ENC_TAG('A', 'D', 'O', 'B' );
const int FXFT_ENCODING_ADOBE_EXPERT = FXFT_ENC_TAG('A', 'D', 'B', 'E' );
const int FXFT_ENCODING_ADOBE_LATIN_1 = FXFT_ENC_TAG('l', 'a', 't', '1' );
const int FXFT_ENCODING_APPLE_ROMAN = FXFT_ENC_TAG('a', 'r', 'm', 'n' );
const int FXFT_ENCODING_ADOBE_CUSTOM = FXFT_ENC_TAG('A', 'D', 'B', 'C' );
const int FXFT_ENCODING_MS_SYMBOL = FXFT_ENC_TAG('s', 'y', 'm', 'b' );
const int FXFT_ENCODING_SJIS = FXFT_ENC_TAG('s', 'j', 'i', 's' );
const int FXFT_ENCODING_GB2312 = FXFT_ENC_TAG('g', 'b', ' ', ' ' );
const int FXFT_ENCODING_BIG5 = FXFT_ENC_TAG('b', 'i', 'g', '5' );
const int FXFT_ENCODING_WANSUNG = FXFT_ENC_TAG('w', 'a', 'n', 's' );
const int FXFT_ENCODING_JOHAB = FXFT_ENC_TAG('j', 'o', 'h', 'a' );

#define FXFT_LOAD_NO_SCALE 0x01
#define FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH 0x200
#define FXFT_LOAD_NO_BITMAP 0x08
#define FXFT_RENDER_MODE_LCD 3
#define FXFT_RENDER_MODE_MONO 2
#define FXFT_RENDER_MODE_NORMAL 0
#define FXFT_LOAD_IGNORE_TRANSFORM 0x800
#define FXFT_PIXEL_MODE_MONO 1
#define FXFT_STYLE_FLAG_ITALIC 1
#define FXFT_STYLE_FLAG_BOLD 2

typedef void* FXFT_Face;
typedef void* FXFT_Library;
typedef void* FXFT_CharMap;
typedef void* FXFT_MM_Var;
typedef void* FXFT_Var_Axis;
typedef void* FXFT_Outline;
typedef void* FXFT_Bitmap;

// basic structures
struct FXFT_Matrix
{
	long xx, xy;
	long yx, yy;
};

struct FXFT_Vector
{
	long x, y;
};
struct FXFT_BBox
{
	unsigned long  xMin, yMin;
    unsigned long  xMax, yMax;
};

typedef enum
{
    FT_LCD_FILTER_NONE    = 0,
	FT_LCD_FILTER_DEFAULT = 1,
	FT_LCD_FILTER_LIGHT   = 2,
	FT_LCD_FILTER_LEGACY  = 16,
		
	FT_LCD_FILTER_MAX		
} FXFT_LcdFilter;

struct FXFT_Outline_Funcs
{
    int (*move_to)(const struct FXFT_Vector* to, void* user);
    int (*line_to)(const struct FXFT_Vector* to, void* user);
    int (*conic_to)(const struct FXFT_Vector* control, const struct FXFT_Vector* to, void* user);
    int (*cubic_to)(const struct FXFT_Vector* control1, const struct FXFT_Vector* control2, const struct FXFT_Vector* to, void* user);
    int shift;
    int	delta;
};

#if !defined(_FPDF_TEXTONLY_)
int FXFT_Init_FreeType(FXFT_Library* library);
int FXFT_Done_FreeType(FXFT_Library library);

int FXFT_New_Memory_Face(FXFT_Library library, const unsigned char* file_base,
                  long file_size, long face_index, FXFT_Face *aface);
int FXFT_New_Face(FXFT_Library library, const char* filename, long face_index, FXFT_Face* face);
int FXFT_Done_Face(FXFT_Face face);
FXFT_Library FXFT_Get_Face_FreeType(FXFT_Face face);
void* FXFT_Alloc(FXFT_Library library, long size);
void FXFT_Free(FXFT_Face face, void* memory);

int FXFT_Select_Charmap(FXFT_Face face, int encoding);
int FXFT_Get_Face_CharmapCount(FXFT_Face face);
FXFT_CharMap* FXFT_Get_Face_Charmaps(FXFT_Face face);
FXFT_CharMap FXFT_Get_Face_Charmap(FXFT_Face face);
int FXFT_Get_Charmap_Encoding(FXFT_CharMap charmap);
int FXFT_Get_Charmap_PlatformID(FXFT_CharMap charmap);
int FXFT_Get_Charmap_EncodingID(FXFT_CharMap charmap);
int FXFT_Set_Charmap(FXFT_Face face, FXFT_CharMap charmap);

int FXFT_Get_Face_UnitsPerEM(FXFT_Face face);
int FXFT_Get_Face_xMin(FXFT_Face face);
int FXFT_Get_Face_xMax(FXFT_Face face);
int FXFT_Get_Face_yMin(FXFT_Face face);
int FXFT_Get_Face_yMax(FXFT_Face face);
int FXFT_Get_Face_Height(FXFT_Face face); 
int FXFT_Get_Face_UnderLineThickness(FXFT_Face face);
int FXFT_Get_Face_UnderLinePosition(FXFT_Face face);
int FXFT_Get_Face_MaxAdvanceWidth(FXFT_Face face);
int FXFT_Get_Face_Ascender(FXFT_Face face);
int FXFT_Get_Face_Descender(FXFT_Face face);

int FXFT_Load_Glyph(FXFT_Face face, int glyph_index, int flags);
int FXFT_Get_Glyph_HoriBearingX(FXFT_Face face);
int FXFT_Get_Glyph_HoriBearingY(FXFT_Face face);
int FXFT_Get_Glyph_HoriAdvance(FXFT_Face face);
int FXFT_Get_Glyph_Width(FXFT_Face face);
int FXFT_Get_Glyph_Height(FXFT_Face face);
int FXFT_Get_Char_Index(FXFT_Face face, int charcode);
int FXFT_Get_Glyph_Name(FXFT_Face face, unsigned int glyph_index, void* buffer, unsigned int buffer_max);
int FXFT_Has_Glyph_Names(FXFT_Face face);
const char* FXFT_Get_Postscript_Name(FXFT_Face face);
int FXFT_Get_Name_Index(FXFT_Face face, char* glyph_name);
int FXFT_Load_Sfnt_Table(FXFT_Face face, unsigned long tag, long offset, unsigned char* buffer, unsigned long* length);
unsigned long FXFT_Get_First_Char(FXFT_Face face, unsigned int* glyph_index);
unsigned long FXFT_Get_Next_Char(FXFT_Face face, unsigned long charcode, unsigned int* glyph_index);

int FXFT_Clear_Face_External_Stream(FXFT_Face face);
int FXFT_Get_Face_External_Stream(FXFT_Face face);
int FXFT_Is_Face_TT_OT(FXFT_Face face);
int FXFT_Is_Face_Tricky(FXFT_Face face);
int FXFT_Is_Face_fixedwidth(face);
const char* FXFT_Get_Face_Stream_Base(FXFT_Face face);
int FXFT_Get_Face_Stream_Size(FXFT_Face face);
const char* FXFT_Get_Face_Family_Name(FXFT_Face face);
int FXFT_Get_Face_Numfaces(FXFT_Face face);
int FXFT_Get_Face_Faceindex(FXFT_Face face);
int FXFT_Is_Face_Italic(FXFT_Face face);
int FXFT_Is_Face_Bold(FXFT_Face face);

int FXFT_Get_MM_Var(FXFT_Face face, FXFT_MM_Var* vars);
FXFT_Var_Axis FXFT_Get_MM_Axis(FXFT_MM_Var var, int index);
int FXFT_Get_MM_Axis_Min(FXFT_Var_Axis axis);
int FXFT_Get_MM_Axis_Max(FXFT_Var_Axis axis);
int FXFT_Get_MM_Axis_Def(FXFT_Var_Axis axis);
int FXFT_Set_MM_Design_Coordinates(FXFT_Face face, int count, long* coords);

int FXFT_Get_Face_Internal_Flag(FXFT_Face face);
void FXFT_Set_Face_Internal_Flag(FXFT_Face face, int flag);

int FXFT_Set_Pixel_Sizes(FXFT_Face, int width, int height);
void FXFT_Set_Transform(FXFT_Face face, struct FXFT_Matrix* matrix, struct FXFT_Vector* delta);
FXFT_Outline FXFT_Get_Glyph_Outline(FXFT_Face face);
int FXFT_Get_Outline_Bbox(FXFT_Outline outline, struct FXFT_BBox* cbox);
int FXFT_Outline_Embolden(FXFT_Outline outline, int strength);
int FXFT_Outline_Decompose(FXFT_Outline outline, struct FXFT_Outline_Funcs* func, void* params);
int FXFT_Render_Glyph(FXFT_Face face, int mode);

FXFT_Bitmap FXFT_Get_Glyph_Bitmap(FXFT_Face face);
int FXFT_Get_Bitmap_Width(FXFT_Bitmap bitmap);
int FXFT_Get_Bitmap_Rows(FXFT_Bitmap bitmap);
int FXFT_Get_Bitmap_PixelMode(FXFT_Bitmap bitmap);
int FXFT_Get_Bitmap_Pitch(FXFT_Bitmap bitmap);
void* FXFT_Get_Bitmap_Buffer(FXFT_Bitmap bitmap);
int FXFT_Get_Glyph_BitmapLeft(FXFT_Face face);
int FXFT_Get_Glyph_BitmapTop(FXFT_Face face);
int FXFT_Library_SetLcdFilter(FXFT_Library library, FXFT_LcdFilter filter);
#endif		// #if !defined(_FPDF_TEXTONLY_)

int FXFT_unicode_from_adobe_name(const char* name);
void FXFT_adobe_name_from_unicode(char* name, unsigned int unicode);
#ifdef __cplusplus
};
#endif
//<<<+++OPENSOURCE_END
#endif		// #if !defined(_EXTERNAL_FREETYPE_) && !defined(_FPDF_TEXTONLY_)
